package com.ficus.road.maintain.controller;

import com.ficus.road.maintain.core.model.GpsLocation;
import com.ficus.road.maintain.core.model.ReplayVideoRequestDTO;
import com.ficus.road.maintain.core.model.ReplayVideoResponseDTO;
import com.ficus.road.maintain.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @Author hao.zhou1
 * @Since 2021/4/7 下午6:47
 */
@RestController
@Slf4j
public class VideoController {

    @Autowired
    private VideoService videoService;

    /**
     * 历史视频回放入口
     *
     * @param carId
     * @param uuid
     * @param replayVideoRequestDTO
     * @return
     */
    @RequestMapping(value = "/video/replay/{carId}/{uuid}",
            method = RequestMethod.POST)
    public ResponseEntity<GpsLocation> replay(@PathVariable("carId") String carId, @PathVariable("uuid") String uuid, @Valid @RequestBody ReplayVideoRequestDTO replayVideoRequestDTO) {
        GpsLocation gpsLocation = videoService.getHistoryLocation(carId, replayVideoRequestDTO.getDate(), replayVideoRequestDTO.getTimestamp());
        //播放历史视频
        log.info("Video: 播放历史视频, carId: {}, uuid: {}, replayVideoRequestDTO: {}", carId, uuid, replayVideoRequestDTO);
        videoService.replayVideo(carId, uuid, replayVideoRequestDTO);
        return new ResponseEntity<>(gpsLocation, HttpStatus.OK);
    }

    @RequestMapping(value = "/video/history",
            method = RequestMethod.POST)
    public ResponseEntity<List<List<ReplayVideoResponseDTO>>> history(@Valid @RequestBody List<String> carIds) {
        List<List<ReplayVideoResponseDTO>> historyVideos = videoService.getHistoryVideos(carIds);
        return new ResponseEntity<>(historyVideos, HttpStatus.OK);
    }

    @RequestMapping(value = "/video/replay/{carId}/{uuid}",
            method = RequestMethod.DELETE)
    public ResponseEntity<Void> stopReplay(@PathVariable("carId") String carId, @PathVariable("uuid") String uuid) {
        videoService.stopReplayVideo(uuid);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
